/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.omg.CORBA.INV_POLICY;

/**
 *
 * @author Nguyen Hong Phu (1142066) - 11HCB (HCMUS)
 */
public final class Paginator extends javax.swing.JPanel implements ActionListener{

    // tổng số records trong database
    private long totalRecords;
    // record hiện tại của database
    private int currentRecord;
    // số records tối đa hiển thị lên table
    private int perRecordsOnPage;

    
    public void init(long totalRecords, int perRecordsOnPage){
        this.totalRecords = totalRecords;
        this.perRecordsOnPage = perRecordsOnPage;
        setCurrentRecord(0);
    }
    
    /**
     * Creates new form Paginator
     */
    public Paginator() {
        initComponents();
        btnFirst.addActionListener(this);
        btnPrev.addActionListener(this);
        btnNext.addActionListener(this);
        btnLast.addActionListener(this);
    }
    
    /**
     * @return the currentRecord
     */
    public int getCurrentRecord() {
        return currentRecord;
    }

    /**
     * @param currentRecord the currentRecord to set
     */
    public void setCurrentRecord(int currentRecord) {
        this.currentRecord = currentRecord;
        // Thay doi label trang hien tai
        lblCurrenPage.setText((currentRecord+1)+" / "+totalRecords);
        // Sau khi set thi cap nhat lai trang thai cac button
        this.updateState();
    }

    public void updateState(){
        if(currentRecord - perRecordsOnPage >= 0){
            // Có trang trước đó
            btnPrev.setEnabled(true);
            btnFirst.setEnabled(true);
        }else{
            // Không có trang trước đó
            btnPrev.setEnabled(false);
            btnFirst.setEnabled(false);
        }
        if(currentRecord + perRecordsOnPage <= totalRecords){
            // Có trang tiếp theo
            btnNext.setEnabled(true);
            btnLast.setEnabled(true);
        }else{
            // Không có trang tiếp theo
            btnNext.setEnabled(false);
            btnLast.setEnabled(false);
        }
    }
    
    public void addEventPerformClick(IPaginatorEvent e){
        this.listenerList.add(IPaginatorEvent.class, e);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
           Object[] listener = listenerList.getListenerList();
        for (int i = 0; i < listener.length; i+=2) {
            // Moi 1 event gom 2 thanh phan: class event và instance event
            if(listener[i] == IPaginatorEvent.class){
                if(e.getSource() == btnFirst){
                    // Set record
                    setCurrentRecord(0);
                    // Fire event
                    ((IPaginatorEvent)listener[i+1]).performEventFirst();
                    
                }else if(e.getSource() == btnPrev){
                    // Set record
                    setCurrentRecord(currentRecord-perRecordsOnPage); 
                    // Fire event
                    ((IPaginatorEvent)listener[i+1]).performEventPrev();
                   
                }else if(e.getSource() == btnNext){
                    // Set record
                    setCurrentRecord(currentRecord+perRecordsOnPage); 
                     // Fire event
                    ((IPaginatorEvent)listener[i+1]).performEventNext();
                    
                }else if(e.getSource() == btnLast){
                     // Set record
                    int current = (int) (totalRecords - Math.ceil(totalRecords % perRecordsOnPage));
                    setCurrentRecord(current);
                     // Fire event
                    ((IPaginatorEvent)listener[i+1]).performEventLast();
                    
                }
                
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnFirst = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        btnLast = new javax.swing.JButton();
        lblCurrenPage = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.white))));

        btnFirst.setText("Đầu");

        btnPrev.setText("Trước");

        btnNext.setText("Sau");

        btnLast.setText("Cuối");

        lblCurrenPage.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblCurrenPage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCurrenPage.setText("1/1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(btnFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnPrev, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblCurrenPage, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                .addGap(12, 12, 12)
                .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnLast, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFirst)
                    .addComponent(btnPrev)
                    .addComponent(btnNext)
                    .addComponent(btnLast)
                    .addComponent(lblCurrenPage))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFirst;
    private javax.swing.JButton btnLast;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.JLabel lblCurrenPage;
    // End of variables declaration//GEN-END:variables


    
}
